package request

import (
	"strings"

	"github.com/go-playground/validator/v10"
)

type Validator interface {
	GetMessages() ValidatorMessages
}

type ValidatorMessages map[string]string

func GetErrorMsg(request interface{}, err error) (string, string) {
	if _, isValidatorErrors := err.(validator.ValidationErrors); isValidatorErrors {
		_, isValidator := request.(Validator)
		for _, v := range err.(validator.ValidationErrors) {
			field := strings.ToLower(v.StructField())
			if isValidator {
				if message, exist := request.(Validator).GetMessages()[v.Tag()]; exist {
					return field, strings.Replace(message, ":label", v.Field(), -1)
				}
			} else {
				return field, v.Error()
			}
		}
	}
	return "", "请传入正确参数"
}
